package com.gao.exp03.controllers;

import com.gao.exp03.models.RegionStats;
import com.gao.exp03.models.ShowDataClass;
import com.gao.exp03.services.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Controller
public class DataController {
    @Autowired
    DataService dataService;

    @GetMapping("/")
    public String getData(Model model){
        ShowDataClass showDataClass=new ShowDataClass(dataService.getDataList(),dataService.getStatistics());
        model.addAttribute("data",showDataClass);
        return "showData";
    }

    @GetMapping("/country")
    public String getCountryData(Model model, String country){
        List<RegionStats> dataList=new ArrayList<RegionStats>();
        for (RegionStats regionStats:dataService.getDataList()) {
            if(regionStats.getCountry().equals(country)){
                dataList.add(regionStats);
            }
        }
        ShowDataClass showDataClass=new ShowDataClass(dataList,dataService.getStatistics());
        model.addAttribute("data",showDataClass);
        return "showCountryData";
    }
}
